package com.fick.util;

import java.util.List;

public class Pager {

	public static String printQueryPage(AjaxPageDTO ajaxPageDTO, String queryString) {
		boolean isFirst = ajaxPageDTO.getIsFirst();
		boolean isLast = ajaxPageDTO.getIsLast();
		int pageCount = ajaxPageDTO.getPageCount();
		int pageId = ajaxPageDTO.getPageId();
		int pageSize = ajaxPageDTO.getPageSize();
		int totalCount = ajaxPageDTO.getTotalCount();
		List<Integer> pages = ajaxPageDTO.getPages();
		StringBuffer str = new StringBuffer("");
		str.append("<form>");
		str.append(getTotalInfo(totalCount, pageSize, pageId, pageCount));
		str.append(getFirstPrevLink(isFirst, pageId, queryString));
		str.append(getPages(pages, pageId, queryString));
		str.append(getNextLastLink(isLast, pageCount, pageId, queryString));
		str.append(getGoInput());
		str.append("</form>");
		str.append(getJavaScript(pageCount, pageId, queryString));
		return str.toString();
	}

	public static String printPage(AjaxPageDTO ajaxPageDTO) {
		// boolean hasNext = ajaxPageDTO.getHasNext();
		// boolean hasPrev = ajaxPageDTO.getHasPrev();
		// String go = ajaxPageDTO.getGo();
		// String hql = ajaxPageDTO.getHql();
		// int listBegin = ajaxPageDTO.getListBegin();
		// int listEnd = ajaxPageDTO.getListEnd();
		// int listStep = ajaxPageDTO.getListStep();
		boolean isFirst = ajaxPageDTO.getIsFirst();
		boolean isLast = ajaxPageDTO.getIsLast();
		int pageCount = ajaxPageDTO.getPageCount();
		int pageId = ajaxPageDTO.getPageId();
		int pageSize = ajaxPageDTO.getPageSize();
		int totalCount = ajaxPageDTO.getTotalCount();
		String queryString = "justList=1";
		List<Integer> pages = ajaxPageDTO.getPages();
		StringBuffer str = new StringBuffer("");
		str.append("<form>");
		str.append(getTotalInfo(totalCount, pageSize, pageId, pageCount));
		str.append(getFirstPrevLink(isFirst, pageId, queryString));
		str.append(getPages(pages, pageId, queryString));
		str.append(getNextLastLink(isLast, pageCount, pageId, queryString));
		str.append(getGoInput());
		str.append("</form>");
		str.append(getJavaScript(pageCount, pageId, queryString));
		return str.toString();
	}

	private static String getTotalInfo(int totalCount, int pageSize,
			int pageId, int pageCount) {
		StringBuffer str = new StringBuffer("");
		str.append("&nbsp;&nbsp;共" + totalCount + "条信息,每页" + pageSize + "条信息");
		str.append("&nbsp;&nbsp;当前第" + pageId + "页/共" + pageCount
				+ "页&nbsp;&nbsp;");
		return str.toString();
	}

	private static String getFirstPrevLink(boolean isFirst, int pageId,
			String queryString) {
		StringBuffer str = new StringBuffer("");
		if (!isFirst) {
			str.append("[<a href=\"?" + queryString + "&go=1\">首　页</a>]");
			str.append("[<a href=\"?" + queryString + "&go=" + (pageId - 1)
					+ "\">上一页</a>]");
		} else {
			str.append("[首　页]");
			str.append("[上一页]");
		}
		return str.toString();
	}

	private static String getPages(List<Integer> pages, int pageId,
			String queryString) {
		StringBuffer str = new StringBuffer("");
		for (int i = 0; i < pages.size(); i++) {
			if (pageId == pages.get(i)) {
				str.append("[<font color=\"red\">" + pages.get(i) + "</font>]");
			} else {
				str.append("[<a href=\"?" + queryString + "&go=" + pages.get(i)
						+ "\">");
				str.append(pages.get(i));
				str.append("</a>]");
			}
		}
		return str.toString();
	}

	private static String getNextLastLink(boolean isLast, int pageCount,
			int pageId, String queryString) {
		StringBuffer str = new StringBuffer("");
		if (!isLast) {
			str.append("[<a href=\"?" + queryString + "&go=" + (pageId + 1)
					+ "\">下一页</a>]");
			str.append("[<a href=\"?" + queryString + "&go=" + pageCount
					+ "\">尾　页</a>]");
		} else {
			str.append("[下一页]");
			str.append("[尾　页]");
		}
		return str.toString();
	}

	private static String getGoInput() {
		StringBuffer str = new StringBuffer("");
		str.append("&nbsp;&nbsp;跳转到第");
		str
				.append("<input type=\"text\" id=\"go\" name=\"go\" style=\"width: 25px;\">");
		str.append("页");
		str
				.append("<input type=\"button\" value=\"go\" onclick=\"checkGo();\">");
		return str.toString();
	}

	private static String getJavaScript(int pageCount, int pageId,
			String queryString) {
		StringBuffer str = new StringBuffer("");
		str.append("\n <script>");
		str.append("\n function checkGo(){");
		str.append("\n var go = document.getElementById('go').value");
		str
				.append("\n if(go == \"\"){alert(\"请输入页码！\");document.getElementById('go').value='';document.getElementById('go').focus();return;}");
		str
				.append("\n else if(go < 1){alert(\"页码范围错误！\");document.getElementById('go').value='';document.getElementById('go').focus();return;}");
		str
				.append("\n else if(!checkNumber(go)){alert(\"请输入数字！\");document.getElementById('go').value='';document.getElementById('go').focus();return;}");
		str
				.append("\n else if(go>parseInt("
						+ pageCount
						+ ")){alert(\"超出页码范围！\");document.getElementById('go').value='';document.getElementById('go').focus();return;}");
		str
				.append("\n else if(go==parseInt("
						+ pageId
						+ ")){alert(\"您输入的就是当前页！\");document.getElementById('go').value='';document.getElementById('go').focus();return;}");
		str.append("\n else{location.href='?" + queryString + "&go='+go}");
		str.append("\n }");
		str.append("\n function checkNumber(v){");
		str.append("\n var m5=/^\\d+$/;return m5.test(v);");
		str.append("\n }");
		str.append("\n </script>");
		return str.toString();
	}
	public static String nprintTable(String page, int currPage, int totalPage,
			int totalCount, String str,String hidden) {// 自己加上的，用于对那些组合查询的页面进行分组使用，传的参数包括整个url
		StringBuilder output=new StringBuilder();
		output.append("<form method=post name=page action='" + page + hidden+"'>\n");
		//output.append(hidden);
		output
				.append("共<span style='font-size: 12px; font-style: normal; color: #FF0000; text-decoration: none; font-weight: bold; font-family: \"宋体\";'>"
						+ totalCount + "</span>个&nbsp;&nbsp;");
		if (currPage <= 1) {
			output.append("首页&nbsp;&nbsp;");
			output.append("上一页&nbsp;&nbsp;");
		} else {
			output.append("<a href='" + page + str
					+ "currPage=1'>首页</a>&nbsp;&nbsp;");
			output.append("<a href='" + page + str + "currPage="
					+ (currPage - 1) + "'>上一页</a>&nbsp;&nbsp;");
		}
		if (currPage != totalPage && totalPage != 0) {
			output.append("<a href='" + page + str + "currPage="
					+ (currPage + 1) + "'>下一页</a>&nbsp;&nbsp;");
			output.append("<a href='" + page + str + "currPage=" + totalPage
					+ "'>最后页</a>&nbsp;&nbsp;");
		} else {
			output.append("下一页&nbsp;&nbsp;");
			output.append("最后页&nbsp;&nbsp;");
		}
		output
				.append("当前第<span style='font-size: 12px; font-style: normal; color: #FF0000; text-decoration: none; font-weight: bold; font-family: \"宋体\";'>"
						+ currPage
						+ "</span>页/共<span style='font-size: 12px; font-style: normal; color: #FF0000; text-decoration: none; font-weight: bold; font-family: \"宋体\";'>"
						+ totalPage + "</span>页&nbsp;&nbsp;");
		/*output.append("到第");
		if (totalPage == 0) {
			output
					.append("<select name=currPage onChange='this.form.submit()' disabled>\n");
		} else {
			output
					.append("<select name=currPage onChange='this.form.submit()'>\n");
		}
		for (int i = 1; i <= totalPage; i++) {
			if (i == currPage)
				output.append("<option value=" + i + " selected>" + i
						+ "</option>\n");
			else
				output.append("<option value=" + i + ">" + i + "</option>\n");
		}
		output.append("</select>页\n");*/
		output.append("</form>\n");
		return output.toString();
	}
}
